<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class AdminIdentity extends CUserIdentity {

    private $_id;

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        $admin = site_administrator::model()->with('rel_administrator_to_administrator_group')->find('administrator_username=:username', array(':username' => $this->username));
        if ($admin === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($admin->administrator_password !== md5($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $admin->administrator_id;
            $model = site_administrator::model()->findByPk($admin->administrator_id);
            $model->administrator_last_login = date("Y-m-d H:i:s");
            $model->save();
            
            //$this->setState('admin', $admin);
            /*
            $this->setState('admin', array(
                'administrator_id' => $admin->administrator_id,
                'administrator_name' => $admin->administrator_name,
                'administrator_image' => $admin->administrator_image,
                'administrator_last_login' => $admin->administrator_last_login,
                'administrator_group_id' => $admin->rel_administrator_to_administrator_group->administrator_group_id,
                'administrator_group_title' => $admin->rel_administrator_to_administrator_group->administrator_group_title,
                'administrator_group_type' => $admin->rel_administrator_to_administrator_group->administrator_group_type
                )
            );
            */
            $this->setState('administrator_id', $admin->administrator_id);
            $this->setState('administrator_name', $admin->administrator_name);
            $this->setState('administrator_image', ($admin->administrator_image)?$admin->administrator_image:"default.jpg");
            $this->setState('administrator_last_login', $admin->administrator_last_login);
            $this->setState('administrator_group_id', $admin->rel_administrator_to_administrator_group->administrator_group_id);
            $this->setState('administrator_group_title', $admin->rel_administrator_to_administrator_group->administrator_group_title);
            $this->setState('administrator_group_type', $admin->rel_administrator_to_administrator_group->administrator_group_type);
            
            $this->errorCode = self::ERROR_NONE;
        }
        return!$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}